/*
 * @Author: 姚潘涛
 * @Date: 2024-04-08 15:18:03
 * @LastEditors: 姚潘涛
 * @LastEditTime: 2024-10-10 16:19:55
 * @Description:
 *
 * Copyright (c) 2024 by pandaman, All Rights Reserved.
 */
#include "xict_driver/task/driver_manager.h"
#include "xict_common/config/version.h"
#include "xict_common/log/log.h"
#include "xict_driver/param/drivers_params.h"
#include "xict_driver/task/gst_camera_driver_task.h"
#include "xict_driver/utils/data_converter.h"
#include "xict_driver/utils/ecal_publisher_manager.h"

namespace xict_driver {
    namespace task {
        DriverManager::DriverManager() {
            std::string project_version = std::string(PROJECT_VERSION);
            ADEBUG_F("[DriverManager] Driver Manager Version < {} >",
                     project_version.c_str());
        }

        void DriverManager::Initialize(
            const std::shared_ptr<xict_driver::param::DriversParams>&
                _driver_params) {
            // 初始化发布器
            ecal_publisher_manager_ =
                std::make_shared<xict_driver::utils::EcalPublisherManager>();
            ecal_publisher_manager_->Initialize(_driver_params);

            data_converter_ =
                std::make_shared<xict_driver::utils::DataConverter>();

            // 初始化任务
            for (const auto& camera_driver_config :
                 _driver_params->camera_driver_configs_) {
                auto camera_task =
                    std::make_shared<xict_driver::task::CameraDriverTask>(
                        camera_driver_config, &task_manager_);
                // camera_task->RegisterDataPubCallback(
                //   std::bind(&DriverManager::PublishCameraData, this,
                //   std::placeholders::_1, std::placeholders::_2));
                camera_task->RegisterDataPubCallback(
                    [this](auto&& PH1, auto&& PH2) {
                        PublishCameraData(std::forward<decltype(PH1)>(PH1),
                                          std::forward<decltype(PH2)>(PH2));
                    });

                tasks_.push_back(camera_task);
            }
        }

        void DriverManager::StartAllTasks() {
            task_manager_.StartAll();
        }

        void DriverManager::StopAllTasks() {
            task_manager_.StopAll();
        }

        void DriverManager::PublishCameraData(const std::string& _task_name,
                                              const cv::Mat& _camera_data) {
            ecal_publisher_manager_->image_publisher_[_task_name].Send(
                data_converter_->ConvertImageMsg(_camera_data));
        }
    }   // namespace task
}   // namespace xict_driver